package com.softer.chengxin.score.controller;

import com.github.pagehelper.PageInfo;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.score.entity.Credit;
import com.softer.chengxin.score.entity.ScoreComposite;
import com.softer.chengxin.score.entity.ScoreCompositeProvince;
import com.softer.chengxin.score.entity.ScoreDetailVO;
import com.softer.chengxin.score.service.SDManagerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author wangixnrui
 * @date 2018/10/12
 */
@RestController
@RequestMapping(value = "/api/ScoreDetail")
public class SDManagerController {

    @Autowired
    private SDManagerService managerService;

    /**
     * 查询企业信用年度评价列表
     *
     * @param scoreComposite 查询实体
     * @param page   页码
     * @return 结果
     */
    @PostMapping(value = "/getYearScoreDetail")
    public PageInfo<ScoreComposite> search(ScoreComposite scoreComposite, Page page, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new PageInfo<>();
        }
        return managerService.getYearScoreDetail(loginName, scoreComposite, page);
    }

    /**
     * 查询省平台推送的
     * 企业信用年度评价列表
     *
     * @param scoreComposite 查询实体
     * @param page           页码
     * @return 结果
     */
    @PostMapping(value = "/getYearScoreDetailFromProvince")
    public PageInfo<ScoreCompositeProvince> getYearScoreDetailFromProvince(ScoreComposite scoreComposite, Page page, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new PageInfo<>();
        }
        return managerService.getYearScoreDetailFromProvince(loginName, scoreComposite, page);
    }
    /**
     * 获取该企业 年度 的 得分详细（优良信息，负面信息，基本信息）
     *
     * @param qyName             企业名
     * @param infoType           信息种类
     * @param effectiveBeginTime 公示开始时间
     * @param effectiveEndTime   公示结束时间
     * @param corpId             企业id
     * @param year               年度
     * @param session            session
     * @param page               页码
     * @return 结果集
     */
    @PostMapping(value = "/getScoreDetailByYear")
    public ManagerPageResult getScoreDetailByYear(String qyName, Integer infoType, String effectiveBeginTime, String effectiveEndTime, Integer corpId, String year, HttpSession session, Page page) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        return managerService.getScoreDetailByYear(loginName, qyName, infoType, effectiveBeginTime, effectiveEndTime, corpId, year, page);
    }
    /**
     * 查询企业信用分数明细
     * 分数详情弹窗
     * 主管端调用
     * @param id 企业Id
     * @return 信用分数明细
     */
    @PostMapping("/getCredit")
    public Credit getCredit(Integer id) {
        return managerService.getCredit(id);
    }

    /**
     * 查询企业信用分数明细
     * 分数详情弹窗
     * 企业端调用
     * @return 信用分数明细
     */
    @PostMapping("/getCorpCredit")
    public Credit getCorpCredit(HttpSession session) {
        // 查询企业ID
        Integer id = (Integer) session.getAttribute(Common.ID);
        if (id == null) {
            return new Credit();
        }
        return managerService.getCredit(id);
    }
    /**
     * 主管部门查看自己管辖区域内企业的 优良/负面信息列表
     *
     * @param infoType 信息种类（优良/负面）
     * @param qyName   企业名称
     * @param proName  项目名称
     * @param state    状态
     * @param page     页码
     * @param session  session
     * @return 结果
     */
    @PostMapping(value = "/GetScoreDetailList")
    public ManagerPageResult getScoreDetailList(Integer infoType, String qyName, String proName, String state, Page page, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        return managerService.getScoreDetailList(loginName, infoType, qyName, proName, state, page);
    }

    /**
     * 审批列表
     *
     * @param infoType 信息种类（优良/负面）
     * @param qyName   企业名称
     * @param proName  项目名称
     * @param page     页码
     * @param session  session
     * @return 结果
     */
    @PostMapping(value = "/getSPList")
    public ManagerPageResult getSPList(Integer infoType, String qyName, String proName, Page page, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        return managerService.getSPList(loginName, infoType, qyName, proName, page);
    }

    /**
     * 删除优良/负面信息  只有市级主管部门有权限
     *
     * @param id      id
     * @param session session
     * @return 结果
     */
    @PostMapping(value = "/deleteScore")
    public CommonResult delete(Integer id, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.PLEASE_LOG_IN);
        }
        return managerService.delete(loginName, id);
    }

    /**
     * 获取负面/优良信息 录入界面 (保存)的信息列表
     *
     * @param qyName   企业名
     * @param proName  项目名
     * @param infoType 信息种类
     * @param page     页码
     * @param session  session
     * @return 结果
     */
    @PostMapping(value = "/getAddScoreList")
    public ManagerPageResult getScoreAddList(String qyName, String proName, Integer infoType, Page page, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        return managerService.getScoreAddList(loginName, qyName, proName, infoType, page);
    }

    /**
     * 主管部门  新增  优良/负面信息
     *
     * @param scoreDetailVO 参数实体
     * @param session       session
     * @return 结果
     */
    @PostMapping(value = "/managerAddScore")
    public CommonResult addScore(ScoreDetailVO scoreDetailVO, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.PLEASE_LOG_IN);
        }
        if (StringUtils.isNotBlank(scoreDetailVO.getBanjiangDate())) {
            scoreDetailVO.setBjsj(scoreDetailVO.getBanjiangDate());
        }
        if (StringUtils.isNotBlank(scoreDetailVO.getEffectiveEndDate())) {
            scoreDetailVO.setEffectiveEndTime(scoreDetailVO.getEffectiveEndDate());
        }
        return managerService.addScore(scoreDetailVO, loginName);
    }

    /**
     * 保存并公示信息
     *
     * @param scoreDetailVO 参数
     * @param session       session
     * @return 结果
     */
    @PostMapping(value = "/saveAndPublicity")
    public CommonResult saveAndPublicity(ScoreDetailVO scoreDetailVO, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.PLEASE_LOG_IN);
        }
        if (StringUtils.isNotBlank(scoreDetailVO.getBanjiangDate())) {
            scoreDetailVO.setBjsj(scoreDetailVO.getBanjiangDate());
        }
        if (StringUtils.isNotBlank(scoreDetailVO.getEffectiveEndDate())) {
            scoreDetailVO.setEffectiveEndTime(scoreDetailVO.getEffectiveEndDate());
        }
        return managerService.addAndPublicity(scoreDetailVO, loginName);
    }

    /**
     * 公示
     *
     * @param id      信息id
     * @param session session
     * @return 结果
     */
    @PostMapping(value = "/publicity")
    public CommonResult publicity(Integer id, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.PLEASE_LOG_IN);
        }
        return managerService.publicity(loginName, id);
    }

    /**
     * 修改保存信息
     *
     * @param scoreDetailVO 参数
     * @param session       session
     * @return 结果
     */
    @PostMapping(value = "/update")
    public CommonResult update(ScoreDetailVO scoreDetailVO, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.PLEASE_LOG_IN);
        }
        if (StringUtils.isNotBlank(scoreDetailVO.getBanjiangDate())) {
            scoreDetailVO.setBjsj(scoreDetailVO.getBanjiangDate());
        }
        return managerService.update(scoreDetailVO, loginName);
    }

    /**
     * 删除保存的信息
     *
     * @param id 信息id
     * @return 结果
     */
    @PostMapping(value = "/deleteSaveScore")
    public CommonResult deleteScore(@RequestParam("id") Integer id) {
        return managerService.delete(id);
    }

    /**
     * 审批优良/负面信息
     *
     * @param id                 信息id
     * @param corpId             企业id
     * @param scoreTypeId        得分 类型id
     * @param score              得分
     * @param effectiveBeginTime 生效开始时间
     * @param effectiveEndTime   生效结束时间
     * @param shyj             审核意见
     * @param state              状态
     * @param session            session
     * @return 结果
     */
    @PostMapping(value = "/sp")
    public CommonResult sp( Integer id,Integer corpId,Integer scoreTypeId, String score, String effectiveBeginTime, String effectiveEndTime, String shyj, Integer state,String backType,Integer scoreTypeDetailId, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.PLEASE_LOG_IN);
        }
        return managerService.sp(loginName, id, corpId, scoreTypeId, score, effectiveBeginTime, effectiveEndTime, shyj, state,backType, scoreTypeDetailId);
    }

    /**
     * 变更优良/负面信息
     *
     * @param scoreDetailVO 参数
     * @param session       session
     * @return 结果
     */
    @PostMapping(value = "/biangeng")
    public CommonResult biangeng(ScoreDetailVO scoreDetailVO,String backType, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), Common.PLEASE_LOG_IN);
        }
        return managerService.biangeng(scoreDetailVO, loginName,backType);
    }

    /**
     * 优良信息得分详情信息
     *
     * @param id 信息id
     * @return 结果
     */
    @PostMapping(value = "/getDetailScore")
    public ScoreDetailVO getDetailScore(Integer id) {
        return managerService.getDetailScore(id);
    }

    /**
     * 获取变更信息列表
     *
     * @param page     页码
     * @param qyName   企业名
     * @param proName  项目名
     * @param infoType 信息种类
     * @param session  sesson
     * @return 结果
     */
    @PostMapping(value = "/getBianGengList")
    public ManagerPageResult getBianGengList(Page page, String qyName, String proName, Integer infoType, HttpSession session) {
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        return managerService.getBianGengList(loginName, page, qyName, proName, infoType);
    }

}
